package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Result order(Map map) throws Exception {
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByorderDate(DateUtils.parseString2Date(orderDate));
        //判断今天有没有设置预约设置
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断今天有没有预约满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断系统有没有该会员
        String telephone = (String) map.get("telephone");
        String idCard = (String) map.get("idCard");
        Member member = memberDao.findByTelephoneandIdCard(telephone, idCard);
        if (member != null) {
            //判断该会员有没有重复预约
            Integer memberId = member.getId();
            Date date = DateUtils.parseString2Date(orderDate);
            int setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(memberId, date, setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList != null && orderList.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //系统没有该会员，就自动添加为会员
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setName((String) map.get("name"));
            member.setRegTime(new Date());
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            memberDao.add(member);
        }
        //添加该预约（预约成功）
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.add(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        return map;
    }


    //================================================================================
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        if (StringUtils.isNotBlank(queryString)) {
            List<Member> members = memberDao.findByname(queryString);
            List<Orderlist> orderlists = new ArrayList<>();
            for (Member member : members) {
                List<Order> orders = orderDao.findBymemberId(member.getId());
                for (Order order : orders) {
                    Setmeal setmeal = setmealDao.findById(order.getSetmealId());
                    Orderlist orderlist = new Orderlist();
                    orderlist.setSetmealName(setmeal.getName());
                    orderlist.setMemberName(member.getName());
                    orderlist.setOrderDate(order.getOrderDate());
                    orderlist.setOrderType(order.getOrderType());
                    orderlist.setOrderStatus(order.getOrderStatus());
                    orderlists.add(orderlist);
                }
            }
            PageInfo<Orderlist> pageInfo = new PageInfo<>(orderlists);
            long total = pageInfo.getTotal();
            List<Orderlist> rows = pageInfo.getList();
            return new PageResult(total, rows);
        } else {
            List<Order> orders = orderDao.findAll();
            List<Orderlist> orderlists = new ArrayList<>();
            for (Order order : orders) {
                Setmeal setmeal = setmealDao.findById(order.getSetmealId());
                Member member = memberDao.findById(order.getMemberId());
                Orderlist orderlist = new Orderlist();
                orderlist.setSetmealName(setmeal.getName());
                orderlist.setMemberName(member.getName());
                orderlist.setOrderDate(order.getOrderDate());
                orderlist.setOrderType(order.getOrderType());
                orderlist.setOrderStatus(order.getOrderStatus());
                orderlists.add(orderlist);
            }
            System.out.println(orderlists);
            PageInfo<Orderlist> pageInfo = new PageInfo<>(orderlists);
            long total = pageInfo.getTotal();
            List<Orderlist> rows = pageInfo.getList();
            return new PageResult(total, rows);
        }
    }
}
